<template>
  <div class="concernsbox">
    <headerComponent title="我的关注"></headerComponent>

    <van-empty
      description=" 还没有关注哦，快去关注吧"
      v-if="list.length == 0"
    />
    <div
      class="concernsitme"
      v-for="(item, index) in list"
      :key="item.id"
      v-else
    >
      <div class="concernCont">
        <img
          :src="$axios.defaults.baseURL + item.head_img"
          alt=""
          v-if="item.head_img"
        />
        <img src="@/assets/logo.png" alt="" v-else />
        <div class="userbox">
          <div class="nickname">{{ item.nickname }}</div>
          <span>{{ item.create_date.split("T")[0] }}</span>
        </div>
        <van-button
          round
          size="small"
          color="linear-gradient(to right,#80bcfd, #2678e3)"
          @click="unfollow(item.id, index)"
          v-if="item.isfollow == true"
        >
          取消关注
        </van-button>
        <van-button
          round
          size="small"
          color="linear-gradient(to right,#f92965,#ab3550)"
          @click="follow(item.id, index)"
          v-else
        >
          关注
        </van-button>
      </div>
    </div>
  </div>
</template>

<script>
import headerComponent from "@/components/HeaderCompon";

export default {
  components: {
    headerComponent
  },
  data() {
    return {
      list: []
    };
  },
  mounted() {
    this.getconcerns();
  },
  methods: {
    getconcerns() {
      this.$axios({
        url: "/user_follows",
        method: "get"
      }).then(res => {
        // this.list = res.data.data;
        const newData = res.data.data.map(res => {
          // 注意：使用map映射的时候一定要return数据出去
          return { ...res, isfollow: true };
        });
        this.list = newData;
        console.log(this.list);
      });
    },
    unfollow(id, index) {
      this.$axios({
        url: "/user_unfollow/" + id
      }).then(res => {
        if (res.data.message == "取消关注成功") {
          this.list[index].isfollow = false;
        }
      });
    },
    follow(id, index) {
      this.$axios({
        url: "/user_follows/" + id
      }).then(res => {
        this.list[index].isfollow = true;
      });
    }
  }
};
</script>

<style lang="less" scoped>
.concernsitme {
  padding: 15px;
  border-bottom: 1px solid #e4e4ee;
  box-sizing: border-box;
  .concernCont {
    display: flex;
    align-items: center;
    img {
      margin-right: 10px;
      width: 70px;
      border-radius: 50%;
    }
    .userbox {
      flex: 1;
      .nickname {
        font-size: 16px;
        color: #222222;
      }
      span {
        color: #b3b3b3;
        font-size: 14px;
      }
    }
  }
}
</style>